﻿using BrightIdeasSoftware;
using SolidWorks.Base;
using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Paine.SolidWorks.Base;

namespace Paine.SolidWorks.Exe
{
    public partial class FrmFeatureTree : Form
    {
        private readonly ModelDoc2 swModel;
        private readonly SldWorks swApp;

        public FrmFeatureTree(SldWorks sw, ModelDoc2 model)
        {
            InitializeComponent();
            swModel = model;
            swApp = sw;
            LoadFeatureNode();
        }
      
        private void btnLoadFeatureTree_Click(object sender, EventArgs e)
        {
            LoadFeatureNode();
        }

        private void LoadFeatureNode()
        {
            try
            {
                swApp.CommandInProgress = true;

                FeatureTreeHelper featureTreeHelper = new FeatureTreeHelper();

                //var feaNodes = featureTreeHelper.GetFeaNodes(swModel);

                var RootFeaNode = featureTreeHelper.GetRootFeaNode(swModel);


                treeListView1.CanExpandGetter = model => ((FeaNode)model).SubNodes.Count > 0;

                treeListView1.ChildrenGetter = delegate(object model) { return ((FeaNode)model).SubNodes; };


                //treeListView1.SetObjects(feaNodes);
                treeListView1.SetObjects(new List<FeaNode>() { RootFeaNode });

                olvName.ImageGetter = new ImageGetterDelegate(ImageGetterDelegate);

             

                //if (RootFeaNode.Expanded)
                //{
                //    treeListView1.Expand(RootFeaNode);
                //}

                ExpandedObject(RootFeaNode);

                //treeListView1.GridLines = false;


                //MessageBox.Show("Down");
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                throw;
            }
            finally
            {
                swApp.CommandInProgress = false;
            }
        }

        private object ImageGetterDelegate(object rowobject)
        {
            FeaNode feaNode=(FeaNode)rowobject;

            //var imagePath = Connect.GetDllPath("") + @"\Images\";



            if (feaNode.IsRoot || feaNode.IsComp)
            {
                if (string.IsNullOrWhiteSpace(feaNode.FileExt))
                {
                    return $@"default.png";
                }
                if (feaNode.FileExt.ToUpper() == ".SLDPRT")
                {
                    return $@"PART.png";
                }
                if (feaNode.FileExt.ToUpper() == ".SLDASM")
                {
                    return $@"Assembly.png";
                }
                if (feaNode.FileExt.ToUpper() == ".SLDDRW")
                {
                    return $@"drawing.png";
                }
            }
            else if (feaNode.IsFeature)
            {
                return $@"{feaNode.FeatureType}.png";
            }
            else
            {
                return   $@"default.png";
            }

            return $@"default.png";
        }

        /// <summary>
        /// 按Solidworks中的原状态显示哪些节点展开。
        /// </summary>
        /// <param name="feaNode"></param>
        private void ExpandedObject(FeaNode feaNode)
        {

            if (feaNode.Expanded)
            {
                treeListView1.Expand(feaNode);

                if (feaNode.SubNodes.Count>0)
                {
                    foreach (var feaNodeSubNode in feaNode.SubNodes)
                    {
                        if (feaNodeSubNode.Expanded)
                        {
                            //treeListView1.Expand(feaNodeSubNode);

                            ExpandedObject(feaNodeSubNode);
                        }
                    }
                }
            }

        }


    }
}
